package com.peng.leetcode.backtracking;

import java.util.*;

/**
 * Combine
 *
 * @author: lupeng6
 * @create: 2020/12/28 20:19
 */
public class Combine {

    public static void main(String[] args) {

    }

    public static List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> result = new ArrayList<>();
        if (n == 0 || k == 0) {
            return result;
        }
        Deque<Integer> deque = new ArrayDeque<>();
        dfs(n, k, 1, deque, result);
        return result;
    }

    public static void dfs(int n, int k, int start, Deque<Integer> path, List<List<Integer>> result) {
        if (path.size() == k) {
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = start; i <= n - (k - path.size()) + 1; i++) {
            path.offer(i);
            dfs(n, k, i + 1, path, result);
            path.removeLast();
        }
    }
}
